package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public BusinessDataVO businessData() {
        //查询今日新增用户
        LocalDate today = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(today, LocalTime.MIN);
        Map map=new HashMap();
        map.put("begin",begin);
        Integer newUser = userMapper.countByMap(map);

        //订单完成率
        //查询总的订单
        Integer total = orderMapper.getCountByMap(map);
        map.put("status", Orders.COMPLETED);
        Integer vaild = orderMapper.getCountByMap(map);
        Double sum = orderMapper.sumByMap(map);
        Double turnover=sum==null?0.0:sum;
        Double orderCompletionRate=0.0;//订单完成率
        Double unitPrice=0.0;//平均客单价
        if(vaild!=0){
            orderCompletionRate=vaild.doubleValue()/total;
            unitPrice=turnover/vaild;
        }
        return BusinessDataVO.builder()
                .newUsers(newUser)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(vaild)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer discontinued = setmealMapper.getCountByStatus(StatusConstant.DISABLE);
        Integer sold = setmealMapper.getCountByStatus(StatusConstant.ENABLE);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        Integer sold = dishMapper.getCountByStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.getCountByStatus(StatusConstant.DISABLE);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDate today = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(today, LocalTime.MIN);
        Map map=new HashMap();
        map.put("begin",begin);
        //全部订单
        List<Orders> orders = orderMapper.getListByMap(map);

        Integer allOrders=0,cancelledOrders=0,completedOrders=0,deliveredOrders=0,waitingOrders=0;

        allOrders=orders.size();

        for (Orders order: orders) {
            Integer status=order.getStatus();
           if(status==Orders.CANCELLED) cancelledOrders++;
           else if(status==Orders.COMPLETED) cancelledOrders++;
           else if(status==Orders.DELIVERY_IN_PROGRESS) deliveredOrders++;
           else if(status==Orders.TO_BE_CONFIRMED) waitingOrders++;
        }

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
